package com.qf.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qf.admin.entity.Goods;
import com.qf.admin.entity.Item;
import com.qf.admin.mapper.IGoodsMapper;
import com.qf.admin.model.TableData;
import com.qf.admin.pojo.AddGoods;
import com.qf.admin.pojo.EditGoods;
import com.qf.admin.pojo.QueryGoods;
import com.qf.admin.service.IGoodsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
@Transactional
public class GoodsServiceImpl implements IGoodsService {

    @Value("${imageLocation}")
    private String imageLocation;

    @Value("${imageServer}")
    private String imageServer;

    private IGoodsMapper goodsMapper;

    public GoodsServiceImpl(IGoodsMapper goodsMapper) {
        this.goodsMapper = goodsMapper;
    }

    @Override
    public TableData<Goods> getGoodsData(QueryGoods queryGoods, Integer offset, Integer limit) {

        TableData<Goods> tableData = new TableData<>();

        Page<Goods> page = PageHelper.offsetPage(offset, limit);

        List<Goods> goodsData = goodsMapper.getGoodsData(queryGoods);

        tableData.setTotal(page.getTotal());

        tableData.setRows(goodsData);

        return tableData;
    }

    @Override
    public void deleteGoodsById(Integer id) {

        goodsMapper.deleteGoodsById(id);

    }

    @Override
    public EditGoods editGoodsById(Integer id) {

        EditGoods editGoods = goodsMapper.editGoodsById(id);

        return editGoods;

    }

    @Override
    public void editGoodsAndSave(EditGoods editGoods) {

        goodsMapper.editGoodsAndSave(editGoods);
    }

    @Override
    public List<Item> getAllItem() {

        List<Item> allItem = goodsMapper.getAllItem();
        return allItem;
    }

    @Override
    public void batchInsert(List<Goods> goodsList) {
        goodsMapper.batchInsert(goodsList);
    }

    @Override
    public List<Goods> getAllGoods(QueryGoods queryGoods) {

        List<Goods> allGoods = goodsMapper.getAllGoods(queryGoods);
        return allGoods;
    }

    @Override
    public void addGoodsMethod(AddGoods addGoods) {
        Integer goodsId = goodsMapper.getGoodsId(addGoods.getGoods_id());
        if (goodsId == null){
            goodsMapper.addGoodsMethod(addGoods);
        }else {
            throw new RuntimeException("商品编号已经存在");
        }



    }



    @Override
    public Map<String, String> updateImage(MultipartFile displayImage, String itemAlias) throws IOException {
        //获取文件的名字
        String filename = displayImage.getOriginalFilename();
        //截取文件的后缀
        String suffix = filename.substring(filename.lastIndexOf("."));
        //生成新的文件名
        String newFileName = UUID.randomUUID().toString() + suffix;

        //添加商品图片的路径
        String filePath = itemAlias + "/" + newFileName;


        //保存图片到本地
        displayImage.transferTo(new File(imageLocation + itemAlias + "/" + newFileName));

        Map<String, String> map = new HashMap<>();
        //在前端展示图片
        map.put("imageUrl", imageServer + itemAlias + "/" + newFileName);
        map.put("filePath",filePath);

        return map;

    }


}
